/**
 * 
 */
package com.idrl.web.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;


import com.idrl.Const;
import com.idrl.utils.SysConfig;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;


public class LoginInterceptor extends AbstractInterceptor
{
	private static Logger log = Logger.getLogger(LoginInterceptor.class);
	private static String[] noInterceptActions = SysConfig.get("noInterceptActions").split(",");
	
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
	 */
	@Override
	public String intercept(ActionInvocation ai) throws Exception
	{
		Map session = ai.getInvocationContext().getSession();
		if(session.get(Const.KEY_USER) == null)
		{
			HttpServletRequest request = (HttpServletRequest)ai.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);   
			request.setAttribute(Const.KEY_INFO_MSG, "请先登录！");
			return "loginByCookie";
		}
		else
		{
			return ai.invoke();
		}
		
//		boolean flag = false;  //true:合法 false:非法
//		if (session.get(Const.KEY_USER) == null)
//		{			
//			String curAction = ai.getInvocationContext().getName();
//			
//			log.debug(curAction);
//			for(String noAction : noInterceptActions)
//			{				
//				if(noAction.equals(curAction))
//				{
//					flag = true;
//					break;
//				}
//			}
//			
//		}
//		else
//		{
//			flag = true;
//		}
//		
//		if(flag)
//		{
//			return ai.invoke();
//		}
//		else //保存请求地址，以便登录成功后转向此地坄1�7
//		{
//			HttpServletRequest request = (HttpServletRequest)ai.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);   
//
//			if(request.getParameter(Const.URL_SAVE_FLAG) != null)
//			{								
//				session.put(Const.URL_INPUT, request.getRequestURL() + "?" + request.getQueryString() ) ;
//			}
//			return "loginByCookie";
//		}
	}
}
